package com.mach.platform.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mach.platform.domain.ProductionOrder;
import com.mach.platform.domain.ProductionOrderTerm;
import com.mach.platform.repository.ProductionOrderTermRepo;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductionOrderTermServiceImpl implements BaseService<ProductionOrderTerm> {

    @Autowired
    private ProductionOrderTermRepo productionOrderTermRepo;
    private static final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public ProductionOrderTerm save(ProductionOrderTerm productionOrderTerm) {
        return null;
    }

    @Override
    public List<ProductionOrderTerm> insertObjInBatch(List<ProductionOrderTerm> list) {
        return null;
    }

    @Override
    public ProductionOrderTerm update(ProductionOrderTerm productionOrderTerm) {
        return null;
    }

    @Override
    public int delete(Long[] ids) {
        return productionOrderTermRepo.deleteByIdIn(ids);
    }

    @Override
    public Page<ProductionOrderTerm> list(Pageable pageable, Map params) {
        if (params.size() > 0){
            Object keyword = params.get("keyword");
            Object startTime = params.get("startTime");
            Object endTime = params.get("endTime");
            if (keyword != null && keyword != "") {
                params.put("productionOrderBan_plike", keyword.toString().trim());
                params.remove("keyword");
            }
            if (startTime != null && startTime != "") {
                params.put("productionOrderTime_gte", LocalDateTime.parse(startTime.toString().trim(), df));
                params.remove("startTime");
            }
            if (endTime != null && endTime != "") {
                params.put("productionOrderTime_lte", LocalDateTime.parse(endTime.toString().trim(), df));
                params.remove("endTime");
            }
        }
        Specification specification = DynamicSpecifications.bySearchFilter(SearchFilter.parse(params).values(), ProductionOrderTerm.class);
        Page<ProductionOrderTerm> result = productionOrderTermRepo.findAll(specification, pageable);
        return result;
    }

    @Override
    public ProductionOrderTerm info(Long id) {
        return null;
    }

    public void add(List<LinkedHashMap> productionOrderTermMap, ProductionOrder productionOrder) {
        List<ProductionOrderTerm> list = new ArrayList<>(32);
        if (productionOrderTermMap.size() > 0){
            productionOrderTermMap.forEach(item -> {
                ProductionOrderTerm productionOrderTerm = JSON.parseObject(JSONObject.toJSONString(item,true),ProductionOrderTerm.class);
                if (StringUtils.isNotBlank(productionOrderTerm.getProductName())) {
                    productionOrderTerm.setProductionOrderTime(productionOrder.getTime());
                    productionOrderTerm.setProductionOrderBan(productionOrder.getClassBan());
                    productionOrderTerm.setProductionOrderId(productionOrder.getId());
                    list.add(productionOrderTerm);
                }
            });
            productionOrderTermRepo.saveAll(list);
        }
    }

    public void update(List<LinkedHashMap> productionOrderTermMap, ProductionOrder productionOrder) {
        //先删除表内容
        //productionOrderTermRepo.deleteByProductionOrderId(productionOrder.getId());
        this.add(productionOrderTermMap, productionOrder);
    }

    public List<ProductionOrderTerm> getListByProductionOrdersIds(List<Long> productionOrderIds) {
        return productionOrderTermRepo.findAllByProductionOrderIdIn(productionOrderIds);
    }
}
